<?php
/**
 * 物物交换
 */
namespace Common\Model;

class ExchangeModel extends BaseModel{
	static $instance=null;
	public static function getItemInstance(){
		if(!self::$instance){
			self::$instance=M('exchange_item',null,DB_MAIN_CFG);
		}
		return self::$instance;
	}

	static $tradeInstance=null;
	public static function getTradeInstance(){
		if(!self::$tradeInstance){
			self::$tradeInstance=M('exchange_record',null,DB_MAIN_CFG);
		}
		return self::$tradeInstance;
	}

	//上架有效时长
	const ITEM_EFFECTIVE_SECOND = 15 * UtilsModel::ONE_DAY;//物品上15天
	const TRADE_EFFECTIVE_SECOND = UtilsModel::ONE_DAY;//交换申请有效期1天


	//用户操作状态
	const ITEM_STATUS_ALL			=0;//全部
	const ITEM_STATUS_AUDIT   		=1;//审核中
	const ITEM_STATUS_ONLINE   		=2;//上架中
	const ITEM_STATUS_IN_TRADING   	=3;//交易中
	const ITEM_STATUS_OFFLINE   	=4;//已下架
	const ITEM_STATUS_SUCCESS   	=5;//成交
	const ITEM_STATUS_AUDIT_REFUSE	=6;//审核被拒

	public static function getItemStatus(){
		return array(
			self::ITEM_STATUS_ALL			=>'全部',
			self::ITEM_STATUS_ONLINE   		=>'上架中',
			self::ITEM_STATUS_IN_TRADING   	=>'匹配中',
			self::ITEM_STATUS_OFFLINE   	=>'已下架',
			self::ITEM_STATUS_SUCCESS   	=>'区配成功',
			self::ITEM_STATUS_AUDIT			=>'审核中',
			self::ITEM_STATUS_AUDIT_REFUSE	=>'审核被拒',
		);
	}

	//物品类型
	const ITEM_TYPE_ALL					=0;//全部
	const ITEM_TYPE_TOY					=1;//玩具
	const ITEM_TYPE_CLOTHES   			=2;//服装
	const ITEM_TYPE_BOOK   				=3;//书具
	const ITEM_TYPE_OTHER   			=4;//其他

	public static function getItemType(){
		return array(
			self::ITEM_TYPE_ALL					=>'全部',
			self::ITEM_TYPE_TOY					=>'玩具',
			self::ITEM_TYPE_CLOTHES   			=>'服装',
			self::ITEM_TYPE_BOOK   				=>'书籍',
			self::ITEM_TYPE_OTHER   			=>'其他'
		);
	}


	//交换状态
	const RECORD_STATUS_ALL				=0;//全部
	const RECORD_STATUS_WAIT			=1;//等待答复
	const RECORD_STATUS_AGREE			=2;//同意
	const RECORD_STATUS_REFUSE			=3;//拒绝,超时也视为拒绝

	const RECORD_DEL_YES	=1;//删除
	const RECORD_DEL_NO		=0;//未删除

	public static function getRecordStatus(){
		return array(
			self::RECORD_STATUS_ALL				=>'全部',
			self::RECORD_STATUS_WAIT			=>'等待答复',
			self::RECORD_STATUS_AGREE			=>'同意',
			self::RECORD_STATUS_REFUSE			=>'拒绝'
		);
	}



	/**
	 * 取我发布的交换信息
	 * @param $uid
	 * @param $page
	 * @param $pageSize
	 * @return array
	 */
	public static function getMine($uid,$page,$pageSize){
		return self::getItemList(self::ITEM_STATUS_ALL,self::ITEM_TYPE_ALL,$page,$pageSize,$uid);
	}




	/**
	 * 获取交换记录
	 * @param $status
	 * @param $page
	 * @param $pageSize
	 * @param int $uid
	 * @return array
	 */
	public static function getTradeList($status,$page,$pageSize,$uid=0,$nickname=null,$beginTime=null,$endTime=null){
		$dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功','phone'=>null);
		$dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
		$now_time=time();
		$tradeM = self::getTradeInstance();

		$where='1';
		if ($status != self::RECORD_STATUS_ALL){
			$where .=" and `status`={$status}";
		}

		if ($uid>0){
			$where .=" and ( (mainUid={$uid} and mainDel=".self::RECORD_DEL_NO.") or (slaveUid={$uid} AND slaveDel=".self::RECORD_DEL_NO.") )";
		}

		if ($nickname){
			$uidArray=UserModel::getInstance()->where("nickname like '%{$nickname}%'")->getField('uid',true);
			$uidStr=implode(',',$uidArray);
			$where .=" and (mainUid in ({$uidStr}) or slaveUid in ({$uidStr}))";
		}

		$beginTimeStamp=0;
		if($beginTime){
			$beginTimeStamp=strtotime($beginTime);
			if($beginTimeStamp>0){
				$where .=" and addTime>={$beginTimeStamp}";
			}
		}

		$endTimeStamp=0;
		if($endTime){
			$endTimeStamp=strtotime($endTime);
			if($endTimeStamp>0){
				$endTimeStamp += UtilsModel::ONE_DAY;
				$where .=" and addTime<{$endTimeStamp}";
			}
		}




		$dataSuccess['total'] = $tradeM->where($where)->count();//获取数据总条数
		$dataSuccess['page'] = $page ? $page : 1;//返回当前页
		$dataSuccess['totalPage'] = ceil($dataSuccess['total']/$pageSize);//总共页数
		$dataSuccess['nextPage'] = $dataSuccess['page'] >= $dataSuccess['totalPage'] ? 0 : 1;//是有有下一页
		$offset=($dataSuccess['page']-1)*$pageSize;


		$exchangeList = $tradeM->where($where)
			->order('id asc')
			->limit($offset,$pageSize)
			->select();


		$itemSet=array();
		$uidSet=array();
		foreach ($exchangeList as $k=>$v){
			$itemSet[$v['mainItem']]	=1;
			$itemSet[$v['slaveItem']]	=1;

			$uidSet[$v['mainUid']]		=1;
			$uidSet[$v['slaveUid']]		=1;
		}



		if(count($itemSet)>0){
			$itemidstr	=implode(',',array_keys($itemSet));
			$itemarray=self::getItemInstance()->where("id in ({$itemidstr})")->field('id,headpic,ownerId,ownerNickname,desc,intention,status')->select();

			$uidArr=array();
			foreach($exchangeList as $k=>$v){
				foreach($itemarray as $one){
					if($v['mainItem']==$one['id']){
						$v['mainHeadPic']=$one['headpic'];
						$v['mainNickname']=$one['ownerNickname'];
						$v['mainStatus']=$one['status'];
						$v['mainDesc']=$one['desc'];
						$v['mainIntention']=$one['intention'];
						$exchangeList[$k]=$v;
					}
					if($v['slaveItem']==$one['id']){
						$v['slaveHeadPic']=$one['headpic'];
						$v['slaveNickname']=$one['ownerNickname'];
						$v['slaveStatus']=$one['status'];
						$v['slaveDesc']=$one['desc'];
						$v['slaveIntention']=$one['intention'];
						$exchangeList[$k]=$v;
					}
				}

				if($v['status']==self::RECORD_STATUS_AGREE){
					$uidArr[$v['mainUid']]=1;
					$uidArr[$v['slaveUid']]=1;
				}
			}
			if(count($uidArr)>0){
				$uidstr	=implode(',',array_keys($uidArr));
				$dataSuccess['phone']=$userPhone=UserModel::getInstance()->where("uid in ({$uidstr})")->getField('uid,bindPhone',true);
			}
		}

		$dataSuccess['data']=$exchangeList;
		return $dataSuccess;
	}




	/**
	 * 获取物品列表数据的基础方法
	 * Author: shijy
	 * @param $userStatus 用户操作状态
	 * @param $auditStatus 审核状态
	 * @param $itemType
	 * @param $page
	 * @param $pageSize
	 * @param int $uid 用户
	 * @return array
	 */
	public static function getItemList($userStatus,$itemType,$page,$pageSize,$uid=0){
		$dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
		$dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
		$now_time=time();

		$where='1';
		if ($userStatus != self::ITEM_STATUS_ALL){
			$where .=" and `status`={$userStatus}";
		}



		if($itemType != self::ITEM_TYPE_ALL){
			$where .=" and itemType={$itemType}";
		}

		if ($uid>0){
			$where .=" and ownerId={$uid}";
		}

		$dataSuccess['total'] = self::getItemInstance()->where($where)->count();//获取数据总条数
		$dataSuccess['page'] = $page ? $page : 1;//返回当前页
		$dataSuccess['totalPage'] = ceil($dataSuccess['total']/$pageSize);//总共页数
		$dataSuccess['nextPage'] = $dataSuccess['page'] >= $dataSuccess['totalPage'] ? 0 : 1;//是否有下一页
		$offset=($dataSuccess['page']-1) * $pageSize;



		$exchangeList = self::getItemInstance()->where($where)
			->field('id,headpic,status,album,desc,intention,itemType,auditTime,auditReason,addTime,ownerNickname')
			->order('addTime desc')
			->limit($offset,$pageSize)
			->select();



		$dataSuccess['data']=$exchangeList;
		return $dataSuccess;
	}


	/**
	 * 是否存在可供交换的宝贝
	 * @param $uid
	 * @return bool
	 */
	public static function haveCanExchangeItem($uid){
		if(self::getItemInstance()->where("ownerId={$uid} and `status`=".self::ITEM_STATUS_ONLINE)->find()){
			return 1;
		}
		return 0;
	}

	public static function getShowItemList($itemStatus,$itemType,$page,$pageSize,$uid=0){
		$dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
		$dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
		$now_time=time();

		$where='1';
		switch ($itemStatus){
			case self::ITEM_STATUS_ALL:
				$allowStatus=array(self::ITEM_STATUS_SUCCESS,self::ITEM_STATUS_ONLINE,self::ITEM_STATUS_IN_TRADING);
				$str=implode(',',$allowStatus);
				$where .=" and `status` in ({$str})" ;
				break;
			case self::ITEM_STATUS_SUCCESS:
				$where .=" and `status`=" .self::ITEM_STATUS_SUCCESS;
				break;
			case self::ITEM_STATUS_ONLINE:
				$where .=" and `status`=" .self::ITEM_STATUS_ONLINE;
				break;
		}




		if($itemType != self::ITEM_TYPE_ALL){
			$where .=" and itemType={$itemType}";
		}

		if ($uid>0){
			$where .=" and ownerId={$uid}";
		}


		$dataSuccess['total'] = self::getItemInstance()->where($where)->count();//获取数据总条数
		$dataSuccess['page'] = $page ? $page : 1;//返回当前页
		$dataSuccess['totalPage'] = ceil($dataSuccess['total']/$pageSize);//总共页数
		$dataSuccess['nextPage'] = $dataSuccess['page'] >= $dataSuccess['totalPage'] ? 0 : 1;//是否有下一页
		$offset=($dataSuccess['page']-1) * $pageSize;


		$effectiveTime=self::ITEM_EFFECTIVE_SECOND;

		$exchangeList = self::getItemInstance()->where($where)
			->field("id,headpic,album,desc,intention,status,ownerId,ownerNickname,itemType,addTime,auditTime+{$effectiveTime}-{$now_time} as leftTime")
			->order('status asc,leftTime DESC')
			->limit($offset,$pageSize)
			->select();

		$dataSuccess['data']=$exchangeList;
		return $dataSuccess;
	}



	/**
	 * 获取详情
	 * Author: shijy
	 * @param $id
	 * @return array
	 */
	public static function getItemDetail($id){
		$dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
		$dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
		if(!$id){
			return $dataFailure;
		}

		$detail = self::getItemInstance()->where("id={$id}")->find();

		if(!$detail){
			$dataFailure['msg']='指定宝贝不存在';
			return $dataFailure;
		}

		switch ($detail['status']){
			case self::ITEM_STATUS_ONLINE://在线状态下，超时物品下架，拒绝交换申请
				$now=time();
				$effectiveTime=self::ITEM_EFFECTIVE_SECOND;
				if( $detail['auditTime'] + $effectiveTime <= $now ){
					$detail['status'] = self::ITEM_STATUS_OFFLINE;
					self::getItemInstance()->where("id={$id}")->setField('status',$detail['status'] );
					self::getTradeInstance()->where("mainItem={$id}")->save( array('status'=>self::RECORD_STATUS_REFUSE,'agreeTime'=>$now ));
				}
				break;
			default:
				break;


		}

		$dataSuccess['data']=$detail;
		return $dataSuccess;
	}



	public static function getTradeDetail($recordId){
		$item=self::getTradeInstance()->find($recordId);
		$now=time();
		switch ($item['status']){
			case self::RECORD_STATUS_WAIT:
				if($item['addTime']+self::TRADE_EFFECTIVE_SECOND <= $now){
					$item['status']=self::RECORD_STATUS_REFUSE;
					self::getTradeInstance()->where("id={$recordId}")->save(
						array(
							'status'=>$item['status'],
							'agreeTime'=>$now
						)
					);
				}
		}
		return $item;
	}


	public static function refuse($recordId,$uid){
		if($recordId<=0){
			return array('status'=>1,'msg'=>'请指定记录');
		}
		$detail=self::getTradeDetail($recordId);
		if(!$detail){
			return array('status'=>1,'msg'=>'没有找到指定的交换记录');
		}
		if($detail['mainUid']!=$uid){
			return array('status'=>1,'msg'=>'交换申请并不是向您发起，您无权操作');
		}
		if($detail['status']!=ExchangeModel::RECORD_STATUS_WAIT){
			return array('status'=>1,'msg'=>'目前的交换状态不可改变');
		}

		ExchangeModel::getTradeInstance()->where('id='.$recordId)->setField('status',ExchangeModel::RECORD_STATUS_REFUSE);
		return array('status'=>0,'msg'=>'success');
	}

	public static function agree($recordId,$uid){
		if($recordId<=0){
			return array('status'=>1,'msg'=>'请指定记录');
		}
		$detail=self::getTradeDetail($recordId);
		if(!$detail){
			return array('status'=>1,'msg'=>'没有找到指定的交换记录');
		}
		if($detail['mainUid']!=$uid){
			return array('status'=>1,'msg'=>'交换申请并不是向您发起，您无权操作');
		}
		if($detail['status']!=ExchangeModel::RECORD_STATUS_WAIT){
			return array('status'=>1,'msg'=>'目前的交换状态不可改变');
		}

		$now=time();
		ExchangeModel::getTradeInstance()->startTrans();
		ExchangeModel::getTradeInstance()->where('id='.$recordId)->save(
			array(
				'status'=>ExchangeModel::RECORD_STATUS_AGREE,
				'agreeTime'=>$now
			)
		);
		ExchangeModel::getItemInstance()->where("id in ({$detail['slaveItem']},{$detail['mainItem']})")->save(array('status'=>ExchangeModel::ITEM_STATUS_SUCCESS,'tradeId'=>$recordId));
		ExchangeModel::getTradeInstance()->where("id <>{$recordId} and mainItem={$detail['mainItem']}")->save(
			array(
				'status'=>ExchangeModel::RECORD_STATUS_REFUSE,
				'agreeTime'=>$now
			)
		);
		ExchangeModel::getTradeInstance()->commit();

		$slavePhone=UserModel::getBindPhone($detail['slaveUid']);
		$mainPhone=UserModel::getBindPhone($detail['mainUid']);

		$slaveItem=ExchangeModel::getItemDetail($detail['slaveItem']);
		SmsModel::sendSms($slavePhone,SmsModel::SMS_TPL_TOY_SUCCESS_WITH_TEL,
			array(
				'item'=>$slaveItem['data']['desc'],
				'phone'=>$mainPhone
			)
		);


		return array('status'=>0,'msg'=>'success','phone'=>$slavePhone);
	}

	public static function onlineOffline($itemId,$uid){
		$itemRet=self::getItemDetail($itemId);
		if($itemRet['status']!=0){
			return $itemRet;
		}
		$item=$itemRet['data'];
		if($item['ownerId']!=$uid){
			$itemRet['status']=1;
			$itemRet['msg']="不是宝贝上架人，不可修改宝贝状态";
			return $itemRet;
		}
		if($item['status']==self::ITEM_STATUS_ONLINE){
			return self::offline($itemId);
		}elseif ($item['status']==self::ITEM_STATUS_OFFLINE){
			return self::online($itemId);
		}
	}


	private static function offline($itemId){
		self::getItemInstance()->where("id={$itemId}")->setField('status',self::ITEM_STATUS_OFFLINE);
		self::getTradeInstance()->where("mainItem={$itemId}")->setField('status',self::RECORD_STATUS_REFUSE);
		return array('status'=>0,'msg'=>'宝贝成功下架','online'=>self::ITEM_STATUS_OFFLINE);
		
	}

	private static function online($itemId){
		self::getItemInstance()->where("id={$itemId}")->save(
			array(
				'status'=>self::ITEM_STATUS_ONLINE,
				'auditTime'=>time()
			)
		);
		return array('status'=>0,'msg'=>'宝贝成功上架','online'=>self::ITEM_STATUS_ONLINE);
	}


	/**
	 * 申请兑换
	 * @param $mainItemId
	 * @param $slaveItemId
	 * @param $slaveUid
	 * @return array|mixed
	 */
	public static function applyDo($mainItemId,$slaveItemId,$slaveUid){
		$mainItem   = ExchangeModel::getItemDetail($mainItemId);
		if($mainItem['status']!=0){
			return $mainItem;
		}
		$mainItem=$mainItem['data'];
		switch ($mainItem['status']){
			case ExchangeModel::ITEM_STATUS_ONLINE:
			case ExchangeModel::ITEM_STATUS_IN_TRADING:
				break;
			default:
				return array('status'=>1,'msg'=>'你期待交换的宝贝当前状态不可交换');
				break;
		}

		$slaveItem  = ExchangeModel::getItemDetail($slaveItemId);
		if($slaveItem['status']!=0){
			return $slaveItem;
		}
		$slaveItem=$slaveItem['data'];

		if($slaveItem['ownerId']!=$slaveUid){
			return array('status'=>1,'msg'=>'你选中的宝贝并不属于您');
		}

		switch ($slaveItem['status']){
			case ExchangeModel::ITEM_STATUS_ONLINE:
				break;
			default:
				return array('status'=>1,'msg'=>'你选中的宝贝当前状态不可交换');
				break;
		}




		ExchangeModel::getItemInstance()->startTrans();
		ExchangeModel::getItemInstance()->where('id='.$slaveItemId)->setField('status',ExchangeModel::ITEM_STATUS_IN_TRADING);
		ExchangeModel::getTradeInstance()->add(
			array(
				'mainItem'=>$mainItemId,
				'mainUid'=>$mainItem['ownerId'],
				'slaveItem'=>$slaveItemId,
				'slaveUid'=>$slaveItem['ownerId'],
				'addTime'=>time(),
				'status'=>ExchangeModel::RECORD_STATUS_WAIT
			)
		);

		NoticeModel::addRedPrompt($slaveItem['ownerId'],NoticeModel::NOTICE_TYPE_TOY);
		ExchangeModel::getItemInstance()->commit();

		$phone=UserModel::getBindPhone($mainItem['ownerId']);
		return SmsModel::sendSms($phone,SmsModel::SMS_TPL_TOY_APPLY,array('item'=>$mainItem['desc']));

	}



	public static function showReason($itemId){
		$itemRet=self::getItemDetail($itemId);
		if($itemRet['status']!=0){
			return $itemRet;
		}
		$reason=self::getItemInstance()->where('id='.$itemId)->getField('auditReason');

		return array('status'=>0,'msg'=>'success','reason'=>$reason);

	}
	/**
	 * 修改、添加宝贝
	 * @param $itemId
	 * @param $uid
	 * @param $nickname
	 * @param $headpic
	 * @param $album
	 * @param $desc
	 * @param $intention
	 * @return array
	 */
	public static function editItem($itemId,$uid,$nickname,$headpic,$album,$desc,$intention,$position,$value){
		$data=array('status'=>1,'msg'=>'error');

		if(true !== $maskStr = checkCleanWord($desc) ){
			$data['msg'] = "宝贝描述中不能包含:".$maskStr;
			return $data;
		}
		if(true !== $maskStr = checkCleanWord($intention) ){
			$data['msg'] = "意向说明中不能包含:".$maskStr;
			return $data;
		}

		if (!$position){
			$data['msg'] = "据在地区必须填写";
			return $data;
		}
		if (!$value){
			$data['msg'] = "物品价值必须填写";
			return $data;
		}


		$albumNum=count($album);
		if($albumNum<2 or $albumNum>5){
			$data['msg'] = "必须上传3-5张图片:";
			return $data;
		}

		$albumStr=implode(',',$album);

		if($itemId>0){//修改
			$oldData=self::getItemInstance()->find($itemId);
			if($oldData['status']!=self::ITEM_STATUS_OFFLINE){
				$data['msg'] = '只有下架的宝贝才可以修改';
				return($data);
			}
			$oldPic=explode(',',$oldData['album']);
			if(count(array_diff($album,$oldPic))>0){//图片有增加、修改，需要审核
				$status=ExchangeModel::ITEM_STATUS_AUDIT;
			}else{//直接审核通过就上线
				$status=ExchangeModel::ITEM_STATUS_ONLINE;
			}

			$saveData=array(
				'desc'      => $desc,
				'intention' => $intention,
				'status'    => $status,
				'headpic'   => $headpic,
				'album'     => $albumStr,
				'position'  => $position,
				'value'  => $value,
			);

			if($status==ExchangeModel::ITEM_STATUS_AUDIT){
				foreach($album as $pic){
					$rt=PornModel::checkPic($pic);
					if($rt==PHOTO_AUDIT_UNPASS){
						$status=ExchangeModel::ITEM_STATUS_AUDIT_REFUSE;
						$saveData['status']=$status;
						$saveData['auditReason'] = '色情图片';
						break;
					}elseif($rt==PHOTO_AUDIT_REVIEW){
						break;
					}elseif($rt==PHOTO_AUDIT_PASS){
						$status=ExchangeModel::ITEM_STATUS_ONLINE;
					}
				}

				if ($status==ExchangeModel::ITEM_STATUS_ONLINE){
					$saveData['status']=$status;
					$saveData['auditTime']=time();
				}
			}

			self::getItemInstance()->where('id='.$itemId)->save($saveData);

		}
		else{//添加
			if (self::getLeftPublishTimes($uid)<=0){
				$data['msg'] = '已经达到一天内允许发布宝贝数量的上限';
				return $data;
			}

			$uinfo = UserModel::getUserChangeFul($uid);
			if($uinfo['status']!=0){
				return $uinfo;
			}
			if($uinfo['data']['point'] < TOY_PUBLISH_NEED_POINT){
				$data['msg']='您的积分不足';
				return $data;
			}



			$albumStr=implode(',',$album);
			$saveData=array(
				'desc'      => $desc,
				'intention' => $intention,
				'status'    => self::ITEM_STATUS_AUDIT,
				'headpic'   => $headpic,
				'album'     => $albumStr,
				'ownerId'   => $uid,
				'ownerNickname' =>$nickname,
				'addTime'   => time(),
				'position'  => $position,
				'value'  => $value,
			);

				$status=ExchangeModel::ITEM_STATUS_ONLINE;
				foreach($album as $pic){
					$rt=PornModel::checkPic($pic);
					if($rt==PHOTO_AUDIT_UNPASS){
						$data['msg'] = "图片中包含色情图片，请重新选择上传";
						return data;
						break;
					}elseif($rt==PHOTO_AUDIT_REVIEW){
						$data['msg'] = "图片中可能含色情图片，需要后台人工审核";
						$status=ExchangeModel::ITEM_STATUS_AUDIT;
						break;
					}elseif($rt==PHOTO_AUDIT_PASS){
					}
				}

				$saveData['status']=$status;
				if ($status==ExchangeModel::ITEM_STATUS_ONLINE){
					$saveData['auditTime']=time();
				}


			$itemId=ExchangeModel::getItemInstance()->add($saveData);
			if($itemId===false){
				$data['msg']=self::getItemInstance()->getError();
				return $data;
			}
			if ($status==ExchangeModel::ITEM_STATUS_ONLINE){
				$ret=UserModel::addUserValByAction($uid,OPT_TOY_PUBLISH);
				if($ret['status']==0){
					$data['tip']=$ret['tip'];
				}
			}

		}

		$data['status']=0;
		$data['itemId']=$itemId;
		return $data;
	}

	/**
	 * 剩余可发布宝贝次数
	 * @param $uid
	 * @return int
	 */
	public static function getLeftPublishTimes($uid){
		list($yesterdayBegin,$yesterdayEnd)=UtilsModel::getYesterDay();
		$leftTimes=TOY_ONEDAY_PUBLISH_LIMIT-ExchangeModel::getItemInstance()->where("ownerId={$uid} and addTime>{$yesterdayEnd}")->count();
		return $leftTimes>0?$leftTimes:0;
	}

	public static function deleteItem($itemId,$uid){
		$itemRet=self::getItemDetail($itemId);
		if($itemRet['status']!=0){
			return $itemRet;
		}
		$item=$itemRet['data'];
		if($item['ownerId']!=$uid){
			$itemRet['status']=1;
			$itemRet['msg']="不是宝贝上架人，不可删除宝贝";
			return $itemRet;
		}

		if($item['status']!=self::ITEM_STATUS_OFFLINE && $item['status']!=self::ITEM_STATUS_AUDIT_REFUSE){
			$itemRet['status']=1;
			$itemRet['msg']="只有已架的宝贝才可以删除";
			return $itemRet;
		}

		self::getItemInstance()->where($itemId)->delete();
		return array('status'=>0,'msg'=>'宝贝成功删除');

	}

	public static function deleteTrade($tradeId,$uid){
		$item=self::getTradeDetail($tradeId);
		if(!$item){
			return array('status'=>1,'msg'=>'记录不存在');
		}
		if($item['mainUid']==$uid ){
			$field='mainDel';
		}
		elseif($item['slaveUid']==$uid){
			$field='slaveDel';
		}
		else{
			return array('status'=>1,'msg'=>'不是您的交换记录，不可删除');
		}

		self::getTradeInstance()->where('id='.$tradeId)->setField($field,self::RECORD_DEL_YES);

		return array('status'=>0,'msg'=>'交换记录成功删除');

	}
}